import Food from './food.js'
import Snake from './snake.js'

class Game {
  constructor(map){
    this.food = new Food()
    this.snake = new Snake()
    this.map = map
    this.timerId = null

  }
  starte(){
    this.food.render(this.map)
    this.snake.render(this.map)
    this.runSnake()
  }
  // 监听键盘事件控制移动方向
  binkey(){

  }
  runSnake(){
    this.timerId = setInterval(() => {
      this.snake.move(this.food,this.map)
      let maxX = this.map.offsetWidth / this.snake.width
      let maxY = this.map.offsetHeight / this.snake.heigh
      let headX = this.snake.body[0].x
      let headY = this.snake.body[0].y
      console.log(maxX)
      console.log(maxY)
      console.log(headX)
      console.log(headY)
      if(headX<0||headX>=maxX||headY<0||headY>=maxY){
        alert('游戏结束')
        clearInterval(this.timerId)
        return
      }
      this.snake.render(this.map)
    }, 400);
  }
}

export default Game
